package com.weirn.mybatis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import static org.apache.poi.ss.usermodel.CellType.*;

/**
 * Created by yp-tc-2563 on 2018/1/26.
 */
public class ExeclUtils {


    public static List<ArrayList<Object>> readExcel(String path){
        File file = new File ( path );

        return readExcel(file);
    }

    public static List<ArrayList<Object>> readExcel(File file){
        if(file == null){
            return null;
        }
        if(file.getName().endsWith("xlsx")){
            //处理ecxel2007
            return readExcel2007(file);
        }else{
            //处理ecxel2003
            return readExcel2003(file);
        }
    }

    public static List<ArrayList<Object>> readExcel2007(File file){
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream ( file );
        } catch ( FileNotFoundException e ) {
            e.printStackTrace ();
            System.out.println ("无效的文件");
            return Collections.EMPTY_LIST;
        }

        try (XSSFWorkbook workbook = new XSSFWorkbook ( file )) {
            XSSFSheet sheet = workbook.getSheetAt ( 0 );
            Iterator<Row> rowIterator =  sheet.iterator ();
            while(rowIterator.hasNext () ){
//                System.out.println ( JSON.toJSONString (rowIterator.next ()));
                XSSFRow row = (XSSFRow) rowIterator.next ();
                Iterator<Cell> cellIterator = row.cellIterator ();
                while(cellIterator.hasNext ()){
                    XSSFCell cell = (XSSFCell) cellIterator.next();
                    String value ;
                    switch (cell.getCellTypeEnum ()){
                        case STRING:
                            value = String.valueOf ( cell.getStringCellValue () );
                            break;
                        case BOOLEAN:
                            value = String.valueOf (cell.getBooleanCellValue ());
                            break;
                        case NUMERIC:
                            value = String.valueOf ( cell.getNumericCellValue () );
                            break;
                        default:
                            value = cell.toString ();
                    }
                    System.out.print(value + "\t");
                }
                System.out.println();
            }
        }catch ( Exception e ){
            System.out.println ();
        }

        return null;
    }
    public static ArrayList<ArrayList<Object>> readExcel2003(File file){

        return null;
    }

    public static void main(String args[]){
        ExeclUtils execlUtils = new ExeclUtils ();
        execlUtils.readExcel("/D/readexecl.xlsx");
    }


}
